<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .container {
            width: 520px;
            height: 280px;
            border: 1px solid red;
            margin: 0 auto;
            position: relative;
            overflow: hidden;
        }

        .content {
            position: absolute;
            left: 0px;
            top: 0px;
            width: 2600px;
            height: 280px;
        }

        img {
            float: left;
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="content">
            <img src="./imgs/1t.png" alt="">
            <img src="./imgs/2t.png" alt="">
            <img src="./imgs/3t.png" alt="">
            <img src="./imgs/4t.png" alt="">
            <img src="./imgs/5t.png" alt="">
        </div>
    </div>
    <script>
        // 做动画的函数
        function move(ele, target, cb) {
            for (let key in target) {
                function run() {
                    // console.log("requestAnimationFrame");
                    let leftNum = parseInt(getComputedStyle(ele, null)[key]);
                    let speed = 10 * (leftNum < target[key] ? 1 : -1);
                    if (leftNum === target[key]) {
                        cb && cb();
                    } else {
                        ele.style[key] = leftNum + speed + "px";
                        // run();
                        requestAnimationFrame(run);
                    }
                }
                run();
            }
        }

        let contentEle = document.querySelector(".content");
        let num = 1; // 当前图片的位置

        // 自动轮播
        autoPlay();
        // 复制第一图 到最后一张；
        copyNodes();
        // 自动轮播
        function autoPlay() {
            setInterval(() => {
                move(contentEle, { left: -num * 520 }, function () {
                    console.log("运动完成");
                    if (num == 6) {
                        // 把容器的left 直接设置成初始位置
                        contentEle.style.left = "0px";
                        num = 1;
                    }
                    console.log(num);
                })
                num++;
            }, 3000);
        }

        // 复制第一张图到第五张图最后
        function copyNodes() {
            // 获取轮播图的第一张
            let firstImgEle = contentEle.firstElementChild;
            //   console.log(firstImgEle)
            // 复制第一张图 
            let copyFirstImgEle = firstImgEle.cloneNode(true);
            contentEle.style.width = "3120px";
            contentEle.appendChild(copyFirstImgEle);
        }





    </script>
</body>

</html>